package org.coding.springbootdemocommandline.semaphore;

import java.util.concurrent.Semaphore;

public class SemaphoreDemo {
    //创建一个计数器值为5的信号量对象,即只能有5个线程同时访问
    Semaphore semaphore = new Semaphore(5);

    public void execute(){
        try {
            //申请许可
            semaphore.acquire();
            try{
                //业务逻辑
            }catch(Exception e){

            }finally {
                //释放许可
                semaphore.release();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
